﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TabPageInfo.cs" company="none">
//
// FlyingTabs, a .NET library to move tab pages in an application.
// Copyright (C) 2012 nant-cross-compile team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 
// </copyright>
// <author>
//   <name>Roland Reinl</name>
//   <mail>reinlroland@googlemail.com</mail>
// </author>
// <history>
//   <historyitem date="2012-07-04" change="Created file"/>
//   <historyitem date="2012-11-16" author="Roland Reinl" change="Code cleanup"/>
// </history>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace FlyingTabsLib
{
  using System.Windows.Forms;

  /// <summary>
  /// This class collects information about and movable tab page.
  /// </summary>
  internal class TabPageInfo
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="TabPageInfo"/> class.
    /// </summary>
    /// <param name="tabPage">The tab page.</param>
    public TabPageInfo(TabPage tabPage)
    {
      this.TabPage = tabPage;
      this.Parent = tabPage.Parent as TabControl;
      this.TabIndex = this.Parent.TabPages.IndexOf(this.TabPage);
    }

    /// <summary>
    /// Gets the tab page.
    /// </summary>
    public TabPage TabPage { get; private set; }

    /// <summary>
    /// Gets the parent.
    /// </summary>
    public TabControl Parent { get; private set; }

    /// <summary>
    /// Gets the index of the tab.
    /// </summary>
    /// <value>
    /// The index of the tab.
    /// </value>
    public int TabIndex { get; private set; }

    /// <summary>
    /// Reverts the movement.
    /// </summary>
    public void RevertMovement()
    {
      TabControl currentParent = this.TabPage.Parent as TabControl;
      if ((currentParent != this.Parent) ||
          (currentParent.TabPages.IndexOf(this.TabPage) != this.TabIndex))
      {
        currentParent.TabPages.Remove(this.TabPage);
        this.Parent.TabPages.Insert(this.TabIndex, this.TabPage);
        this.Parent.SelectedTab = this.TabPage;
      }
    }
  }
}
